package com.eascs.web.p2p.loginAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.entity.AdminUserRoleEntity;
import com.eascs.p2p.common.service.IAdminUserService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;

@RestController
@RequestMapping("/admin/user/Rest")
public class AdminRestController extends BaseRestController {

	@Reference
	IAdminUserService adminUserService;
	


	@RequestMapping(value = "/saveAdmin")
	public BaseRestResult<String> saveAdmin(AdminUserEntity vo) {

		
		if (StringUtil.isNotBlank(vo.getId())) {
        
			ModelResult<AdminUserEntity> adminModel=adminUserService.queryAdminUserById(vo.getId());
			AdminUserEntity admin=new AdminUserEntity();
			if(adminModel.isSuccess()){
				
				admin=adminModel.getModel();
			}
			
			admin.setName(vo.getName());
			admin.setMobile(vo.getMobile());
			admin.setPassword(vo.getPassword());
			ModelResult<Boolean> model = adminUserService.updateAdminUser(admin);
			if (model.isSuccess()) {
				if (model.getModel()) {

					return BaseRestResult.warn("修改成功").andRedirect("/admin/user/index");
				} else {
					return BaseRestResult.error("数据库操作失败");
				}
			} else {
				return BaseRestResult.info("修改失败");
			}
		} else {
			AdminUserEntity userModel=	adminUserService.getUserByAccount(vo.getAccount());
			if(userModel!=null){
				return BaseRestResult.warn("用户名已经存在,不能重复用户名");
			}
			ModelResult<String> savemodel = adminUserService.saveAdminUser(vo);
			if (savemodel.isSuccess()) {
				if (StringUtil.isNotBlank(savemodel.getModel())) {

					return BaseRestResult.warn("添加成功").andRedirect("/admin/user/index");
				} else {
					return BaseRestResult.error("数据库操作失败");
				}
			} else {
				return BaseRestResult.info("添加失败失败");
			}
		}
	}

	/**
	 * @Title: saveUserRole
	 * @Description:保存用户的角色
	 * @author lwx
	 * @param userId，roleId
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/saveUserRole")
	public BaseRestResult<String> saveUserRole(String userId,String roleId,HttpServletRequest request) {
		
		
		ModelResult<Boolean> model=adminUserService.isExistUserRole(userId,roleId);
		if(model.isSuccess()){
			if(model.getModel()){
				return BaseRestResult.warn("用户已经拥有该角色");
			}else{
				HttpSession session =request.getSession();
				AdminUserEntity userName=(AdminUserEntity) session.getAttribute("adminUser");
				AdminUserRoleEntity adminUserRoleEntity=new AdminUserRoleEntity();
				adminUserRoleEntity.setAdminId(userId);
				adminUserRoleEntity.setRoleId(roleId);
				adminUserRoleEntity.setCreator(userName!=null?userName.getAccount():"0");
				adminUserRoleEntity.setUpdator(userName!=null?userName.getAccount():"0");
				ModelResult<String> saveModel= adminUserService.saveUserRole(adminUserRoleEntity);
				if(saveModel.isSuccess()){
					if(StringUtil.isNotBlank(saveModel.getModel())){
						return	BaseRestResult.info("保存成功").andRefresh();
					}else{
						return BaseRestResult.error("数据库操作失败");
					}
				}else{
					return BaseRestResult.error("保存失败");
				}
			}
		}else{
			return BaseRestResult.error("数据库操作失败");
		}
	}
	/**
	 * @Title: delUserRole
	 * @Description:删除用户的角色
	 * @author lwx
	 * @param userId，roleId
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/delUserRole")
	public BaseRestResult<String> delUserRole(String userId,String roleId) {
		ModelResult<Boolean> model=adminUserService.delUserRole(userId,roleId);
		if(model.isSuccess()){
			if(model.getModel()){
				;
				BaseRestResult.info("操作成功");
				return BaseRestResult.refresh();
			}else{
				return BaseRestResult.warn("操作失败");
			}
		}else{
			return BaseRestResult.error("数据库操作失败");
		}
	}
	
	
	
	
	
	/**
	 * @Title: updateStatus
	 * @Description: 用户禁用或者启用
	 * @author 张庆奇
	 * @param id
	 * @param status
	 * @return
	 * @return BaseRestResult<String>
	 * @throws
	 * @date 2016年8月3日 下午2:06:56
	 */
	@RequestMapping(value="/updateStatus")
	public BaseRestResult<String>  updateStatus(String id,int status) {
		BaseRestResult<String> result = null;
		
			ModelResult<AdminUserEntity> model = adminUserService.queryAdminUserById(id);
			AdminUserEntity entity=new AdminUserEntity();
			if (model.isSuccess()) {
				entity=model.getModel();
				entity.setStatus(status);
				ModelResult<Boolean> upModel=adminUserService.updateAdminUser(entity);
				if (upModel.getModel()) {
					result = BaseRestResult.warn("操作成功").andRedirect("/admin/user/index");
					
				}else{
					result = BaseRestResult.error("操作失败");
				}

			}else{
				
				result = BaseRestResult.error("操作失败,数据库发生异常");
			}
			

		
		return result;
	}
	
	
	
	
	
	
	
	
	

}
